package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import artid.com.arttogoa.R;

import artid.com.arttogoa.adapter.MyArtPush_Adapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.IbeaconActivityInfoParser;
import artid.com.arttogoa.parser.MyArtPushParser;
import artid.com.arttogoa.parser.MyArtPush_IbactivityListBean;
import artid.com.arttogoa.parser.MyArtPush_IbeaconListBean;
import artid.com.arttogoa.util.ToastUtil;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout.OnRefreshListener;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullableListView;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：MyArtPush_Activity   
 * 类描述   ：我的艺推   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-11-7 上午10:17:08   
 *
 */
public class MyArtPush_Activity extends BaseActivity implements OnClickListener{
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page=1;
	private LinearLayout noDataLL;
	private TextView noDataHint;
	private MyArtPush_Adapter adapter;
	private List<MyArtPush_IbeaconListBean> list;
	private List<MyArtPush_IbeaconListBean> more_list;
	private List<MyArtPush_IbactivityListBean> last_list;
	private ImageView hint_icon;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myartpush);
		setContent(this, getIntent());
		mTittle.setText("我的艺推");
		setBack();
		mContext=this;
		initView();
	}

	private void initView(){
		// 注册当前广播
		IntentFilter filter = new IntentFilter("com.MyArtPush_Activity.receiver");
		registerReceiver(broadcastReceiver, filter);
		list=new ArrayList<MyArtPush_IbeaconListBean>();
		more_list=new ArrayList<MyArtPush_IbeaconListBean>();
		hint_icon = (ImageView) findViewById(R.id.release_icon);
		hint_icon.setImageResource(R.drawable.yit_ico_help);
		hint_icon.setVisibility(View.VISIBLE);
		hint_icon.setOnClickListener(this);
		noDataLL=(LinearLayout) findViewById(R.id.include_noDataLL);
		noDataHint=(TextView) findViewById(R.id.include_noData_hint);
		noDataHint.setText("哎呦，没有ibeacon设备哦！");
		mListView =(PullableListView) findViewById(R.id.MyArtPush_listivew);
		mListView.setOnItemClickListener(onItemClick);
		mPullLayout = (PullToRefreshLayout)findViewById(R.id.MyArtPush_refresh_view);
		mPullLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				//下拉刷新
				page=1;
				isRefreshed=true;
				getData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				//上拉加载
				page++;
				getData();
			}
		}); 
		getData();
	}

	OnItemClickListener onItemClick=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			if(!StringUtils.isEmpty(last_list.get(position).getIbactivitytitle())){
				getActivityInfo(last_list.get(position).getIbactivityid(),position);
			}
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.release_icon:
			//说明
			Intent i=new Intent(mContext,MyArtPushExplain_Activity.class);
			startActivity(i);
			break;

		default:
			break;
		}
	}

	private void getData(){
		VolleyBasic.getInstance(this).post(Cache.ibeaconList,
				VolleyMap.getInstance().ibeaconList(page,5),
				MyArtPushParser.class, new VolleyCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				last_list=new ArrayList<MyArtPush_IbactivityListBean>();
				VolleyBasic.disMissLoding();
				System.out.println(vd.getJson());
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				MyArtPushParser bean=(MyArtPushParser) vd.getData();
				if(bean!=null){
					list=bean.getIbeaconlist();
				}
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (list!=null && list.size()!=0) {
					more_list.addAll(list);
				}
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
				getLastData();
				if (adapter == null) {
					adapter=new MyArtPush_Adapter(mContext, last_list);
					mListView.setAdapter(adapter);
				}
				adapter.notifyData(last_list);
			}
			public void onError(VolleyError volleyError) {
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
				VolleyBasic.disMissLoding();
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
			}
		});
	}

	/**
	 * 获取最后的数据
	 */
	private void getLastData(){

		if(more_list!=null && more_list.size()!=0){
			for (int i = 0; i < more_list.size(); i++) {
				if(more_list.get(i).getIbactivitylist()!=null && more_list.get(i).getIbactivitylist().size()!=0){
					for (int j = 0; j < more_list.get(i).getIbactivitylist().size(); j++) {
						MyArtPush_IbactivityListBean bean=new MyArtPush_IbactivityListBean();
						bean.setIbeaconid(more_list.get(i).getIbeaconid());
						bean.setIbeaconnumber(more_list.get(i).getIbeaconnumber());
						bean.setIbactivityid(more_list.get(i).getIbactivitylist().get(j).getIbactivityid());
						bean.setIbactivitytitle(more_list.get(i).getIbactivitylist().get(j).getIbactivitytitle());
						bean.setIbactivityimage(more_list.get(i).getIbactivitylist().get(j).getIbactivityimage());
						bean.setAddtime(more_list.get(i).getIbactivitylist().get(j).getAddtime());
						bean.setStatus(more_list.get(i).getIbactivitylist().get(j).getStatus());
						bean.setIbeacon_ranking(more_list.get(i).getIbeaconnumber());
						if(j==0){
							//判断是否是该组的第一条数据
							bean.setIbeacon_title(more_list.get(i).getIbeaconnumber());
						}else{
							bean.setIbeacon_title("");
						}
						if(j==more_list.get(i).getIbactivitylist().size()-1){
							//判断是否是该组的最后一条数据
							bean.setIsLastData(true);
						}else{
							bean.setIsLastData(false);
						}
						last_list.add(bean);
					}
				}else{
					//没有绑定的设备
					MyArtPush_IbactivityListBean bean=new MyArtPush_IbactivityListBean();
					bean.setIbeaconid(more_list.get(i).getIbeaconid());
					bean.setIbeaconnumber(more_list.get(i).getIbeaconnumber());
					bean.setIbactivityid("空数据");
					bean.setIbactivitytitle("");
					bean.setIbactivityimage("");
					bean.setAddtime("");
					bean.setStatus("");
					bean.setIbeacon_title(more_list.get(i).getIbeaconnumber());
					bean.setIbeacon_ranking(more_list.get(i).getIbeaconnumber());
					bean.setIsLastData(true);
					last_list.add(bean);
				}
			}
		}
	}

	/**
	 * 获取Ibenacon活动信息
	 */
	private void getActivityInfo(String ibactivityid,final int position){
		VolleyBasic.getInstance((Activity)mContext).post(Cache.ibactivityInfo,
				VolleyMap.getInstance().ibactivityInfo(ibactivityid),
				IbeaconActivityInfoParser.class, new VolleyCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				IbeaconActivityInfoParser bean=(IbeaconActivityInfoParser) vd.getData();
				Intent i=new Intent(mContext,IbeaconActivityInfo_Activity.class);
				i.putExtra("IbeaconActivityInfoParser", bean);
				i.putExtra("MyArtPush_IbactivityListBean", last_list.get(position));
				mContext.startActivity(i);
			}

			public void onError(VolleyError volleyError) {
				ToastUtil.toast(mContext, "操作失败，请重试");
			}
		});
	}

	// 广播监听
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getBooleanExtra("isRefresh", false) == true) {
				page=1;
				isRefreshed=true;
				getData();
			}
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 关闭广播
		unregisterReceiver(broadcastReceiver);
	}
}
